package com.uphone.magicleafteacher.pro.activity;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;

import com.radish.baselibrary.Intent.IntentUtils;
import com.uphone.magicleafteacher.MyApp;
import com.uphone.magicleafteacher.R;
import com.uphone.magicleafteacher.base.BaseListActivity;
import com.uphone.magicleafteacher.http.ApiService;
import com.uphone.magicleafteacher.pro.adapter.AdviserAdapter;
import com.uphone.magicleafteacher.util.CommonUtil;
import com.uphone.magicleafteacher.util.GsonUtils;
import com.uphone.magicleafteacher.vo.AdviserBean;
import com.uphone.magicleafteacher.vo.AdviserEvent;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import io.reactivex.Observable;
import okhttp3.ResponseBody;

/**
 * 指导检测
 */

public class AdviserActivity extends BaseListActivity<AdviserBean.StuAppointmentsBean, AdviserAdapter> {

    private String studentName = "";

    @Override
    protected void initLayoutBefore() {
        super.initLayoutBefore();
        isEventBus = true;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onRefresh(AdviserEvent outGroupEvent) {
        //刷新
        initData();
    }

    @Override
    protected Observable<ResponseBody> loadData(ApiService apiService) {
        return MyApp.apiService(ApiService.class)
                .studentAppointment(CommonUtil.getTeacherId(), page + "", "10", CommonUtil.getStr(studentName));
    }

    @Override
    protected List<AdviserBean.StuAppointmentsBean> getDataList(String json) {
        AdviserBean bean = GsonUtils.getGson().fromJson(json, AdviserBean.class);
        if (bean != null)
            return bean.getStuAppointments();
        return null;
    }

    @Override
    protected void itemClick(AdviserAdapter adapter, View view, int position) {
        if (mAdapter.getItem(position).getTestStatus() != 2) {
            IntentUtils.getInstance().with(this, TestDetailsActivity.class)
                    .putInt("status", mAdapter.getItem(position).getTestStatus())
                    .putInt("id", mAdapter.getItem(position).getAppointStuId())
                    .start();
        }
    }

    @Override
    protected AdviserAdapter initAdapter() {
        return new AdviserAdapter();
    }

    @Override
    protected void initBundle() {

    }

    @Override
    protected void initTitle() {
        initTitle("指导检测");
        View searchView = View.inflate(this, R.layout.layout_search, null);
        EditText etSearch = searchView.findViewById(R.id.et_search);
        ll.addView(searchView,0);
        etSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                setShowLoading(false);
                studentName = CommonUtil.getStr(s.toString());
                initData();
            }
        });
    }

}
